//
//  JKRFeedCommentLayout.m
//  JKRFeedDemo
//
//  Created by Lucky on 2017/12/9.
//  Copyright © 2017年 Lucky. All rights reserved.
//

#import "JKRFeedCommentLayout.h"

@implementation JKRFeedCommentLayout

- (instancetype)initWithCommentModel:(JKRFeedCommentModel *)model {
    self = [super init];
    self.model = model;
    [self _layout];
    return self;
}

- (void)_layout {
    self.topLineFrame = CGRectMake(0, 0, kScreenWidth, 0.5);
    self.headerImageViewFrame = CGRectMake(15, CGRectGetMaxY(self.topLineFrame) + 10, kFeedCommentHeaderImageWH, kFeedCommentHeaderImageWH);
    
    YYTextBorder *highlightBorder = [YYTextBorder new];
    highlightBorder.insets = UIEdgeInsetsMake(-2, 0, -2, 0);
    highlightBorder.cornerRadius = 3;
    highlightBorder.fillColor = UIColorHex(bfdffe);
    
    {
        NSMutableAttributedString *timeString = [[NSMutableAttributedString alloc] initWithString:self.model.date attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kFeedCommentTimeFontSize], NSForegroundColorAttributeName:kFeedCommentTimeColor}];
        YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(82, MAXFLOAT)];
        container.maximumNumberOfRows = 1;
        self.timeLabelLayout = [YYTextLayout layoutWithContainer:container text:timeString];
        self.timeLabelFrame = CGRectMake(kScreenWidth - self.timeLabelLayout.textBoundingSize.width - 15, CGRectGetMaxY(self.topLineFrame) + 9, self.timeLabelLayout.textBoundingSize.width, 16);
    }
    
    {
        YYTextHighlight *highLight = [YYTextHighlight new];
        [highLight setBackgroundBorder:highlightBorder];
        [highLight setUserInfo:@{kHightLightTapUser:self.model.author.uid}];
        NSMutableAttributedString *nameString = [[NSMutableAttributedString alloc] initWithString:self.model.author.username attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kFeedCommentNameFontSize], NSForegroundColorAttributeName:kFeedCommentNameColor}];
        [nameString setTextHighlight:highLight range:nameString.rangeOfAll];
        YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(kScreenWidth - 55 - self.timeLabelFrame.size.width - 15 - 10, MAXFLOAT)];
        container.maximumNumberOfRows = 1;
        self.nameLabelLayout = [YYTextLayout layoutWithContainer:container text:nameString];
        self.nameLabelFrame = CGRectMake(55, CGRectGetMaxY(self.topLineFrame) + 9, self.nameLabelLayout.textBoundingRect.size.width, 16);
    }
    
    {
        if (self.model.reply_user) {
            NSMutableAttributedString *replayString = [NSMutableAttributedString new];
            NSMutableAttributedString *header = [[NSMutableAttributedString alloc] initWithString:@"回复 " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kFeedCommentContentFontSize], NSForegroundColorAttributeName:kFeedCommentContentColor}];
            NSMutableAttributedString *replayName = [[NSMutableAttributedString alloc] initWithString:self.model.reply_user.username attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kFeedCommentNameFontSize], NSForegroundColorAttributeName:kFeedCommentNameColor}];
            NSMutableAttributedString *content = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %@", self.model.content] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kFeedCommentContentFontSize], NSForegroundColorAttributeName:kFeedCommentContentColor}];
            [replayString appendAttributedString:header];
            [replayString appendAttributedString:replayName];
            [replayString appendAttributedString:content];
            
            NSRange searchRange = NSMakeRange(0, replayString.string.length);
            YYTextHighlight *highLightName = [YYTextHighlight new];
            [highLightName setBackgroundBorder:highlightBorder];
            [highLightName setUserInfo:@{kHightLightTapUser:self.model.reply_user.uid}];
            NSRange highLightNameRange = [replayString.string rangeOfString:replayName.string options:kNilOptions range:searchRange];
            [replayString setTextHighlight:highLightName range:highLightNameRange];
            
            YYTextHighlight *highLightContent = [YYTextHighlight new];
            [highLightContent setBackgroundBorder:highlightBorder];
            [highLightContent setUserInfo:@{kHightLightTapFeedComment:self.model.slug}];
            NSRange highLightConentRange = [replayString.string rangeOfString:content.string options:kNilOptions range:searchRange];
            [replayString setTextHighlight:highLightContent range:highLightConentRange];
            
            replayString.lineBreakMode = NSLineBreakByCharWrapping;
            
            YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(kScreenWidth - 55 - 15, MAXFLOAT)];
            self.commentLabelLayout = [YYTextLayout layoutWithContainer:container text:replayString];
        } else {
            NSMutableAttributedString *conentString = [[NSMutableAttributedString alloc] initWithString:self.model.content attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kFeedCommentContentFontSize], NSForegroundColorAttributeName:kFeedCommentContentColor}];
            YYTextHighlight *highLightContent = [YYTextHighlight new];
            [highLightContent setBackgroundBorder:highlightBorder];
            [highLightContent setUserInfo:@{kHightLightTapFeedComment:self.model.slug}];
            [conentString setTextHighlight:highLightContent range:NSMakeRange(1, conentString.length - 1)];
            YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(kScreenWidth - 55 - 15, MAXFLOAT)];
            self.commentLabelLayout = [YYTextLayout layoutWithContainer:container text:conentString];
        }
    }
    self.commentLabelFrame = CGRectMake(55, CGRectGetMaxY(self.nameLabelFrame) + 2, self.commentLabelLayout.textBoundingRect.size.width, self.commentLabelLayout.textBoundingRect.size.height);
    
    self.cellHeight = CGRectGetMaxY(self.commentLabelFrame) + 10;
}

@end
